package com.dmap.knowledge.service.impl;

import com.dmap.knowledge.controller.api.param.ArticleHistoryParam;
import com.dmap.knowledge.dao.ArticleBrowseHistoryMapper;
import com.dmap.knowledge.po.Article;
import com.dmap.knowledge.po.ArticleBrowseHistory;
import com.dmap.knowledge.po.ArticleBrowseHistoryExample;
import com.dmap.knowledge.service.ArticleExtensionService;
import com.dmap.knowledge.service.ArticleHistoryService;
import com.dmap.knowledge.service.ArticleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author zmh
 * @date 18-7-17 下午5:40
 */

@Service
public class ArticleHistoryServiceImpl implements ArticleHistoryService {
    @Resource
    private ArticleBrowseHistoryMapper articleBrowseHistoryMapper;
    @Resource
    private ArticleService articleService;
    @Resource
    private ArticleExtensionService articleExtensionService;
    
    @Override
    public int insertArticle(ArticleHistoryParam articleHistoryParam) {
        ArticleBrowseHistory articleBrowseHistory = new ArticleBrowseHistory();
        if (articleHistoryParam.getArticleId() != null) {
            articleBrowseHistory.setArticleId(Long.parseLong(articleHistoryParam.getArticleId()));
        }
        articleBrowseHistory.setBrowseTime(System.currentTimeMillis() / 1000);
        if (articleHistoryParam.getUserId() != null) {
            articleBrowseHistory.setUserId(Long.parseLong(articleHistoryParam.getUserId()));
        }
        Article article = articleService.selectByArticleId(Long.parseLong(articleHistoryParam.getArticleId()));
        if (article == null) {
            return 0;
        }
        //如果存在就删除
        if(countArticleHistory(articleHistoryParam)>0){
            deleteArticleHistory(articleHistoryParam);
        }
        //更新阅读量
        articleExtensionService.updateArticleNumber(Long.parseLong(articleHistoryParam.getArticleId()));
        return articleBrowseHistoryMapper.insertSelective(articleBrowseHistory);
    }
    
    @Override
    public int countArticleHistory(ArticleHistoryParam articleHistoryParam) {
        ArticleBrowseHistoryExample articleBrowseHistoryExample = new ArticleBrowseHistoryExample();
        ArticleBrowseHistoryExample.Criteria criteria = articleBrowseHistoryExample.createCriteria();
        criteria.andArticleIdEqualTo(Long.parseLong(articleHistoryParam.getArticleId()));
        criteria.andUserIdEqualTo(Long.parseLong(articleHistoryParam.getUserId()));
        return articleBrowseHistoryMapper.countByExample(articleBrowseHistoryExample);
    }
    
    @Override
    public int deleteArticleHistory(ArticleHistoryParam articleHistoryParam) {
        ArticleBrowseHistoryExample articleBrowseHistoryExample = new ArticleBrowseHistoryExample();
        ArticleBrowseHistoryExample.Criteria criteria = articleBrowseHistoryExample.createCriteria();
        criteria.andArticleIdEqualTo(Long.parseLong(articleHistoryParam.getArticleId()));
        criteria.andUserIdEqualTo(Long.parseLong(articleHistoryParam.getUserId()));
        return articleBrowseHistoryMapper.deleteByExample(articleBrowseHistoryExample);
    }
}
